/*
========================================================================
 Name        : Test9Container.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikappui.h>
#include <Test9.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "Test9Container.h"
#include "Test9ContainerView.h"
#include "Test9.hrh"
#include "Test9Container.hrh"
#include "Test9ListBox.hrh"
#include "Test9Container2.hrh"
// ]]] end generated region [Generated User Includes]
#include "Test9AppUi.h"
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CTest9Container::CTest9Container() : NewTrip(0), BuildingPath(1), IsUpkeyForward(true)
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	IsPaused = false;
	ApplicationMode = NewTrip;
	view = ((CTest9AppUi*)iEikonEnv->AppUi())->iTest9ContainerView;
								//   C:\ Data\ Sounds\ Walkassist\ notify.wav
	//startSound = CSoundPlayer::NewL(_L("C:\\Data\\Sounds\\Walkassist\\notify.wav"));
	//rightSound = CSoundPlayer::NewL(_L("C:\\Data\\Sounds\\Walkassist\\English\\right.wav"));
	//leftSound = CSoundPlayer::NewL(_L("C:\\Data\\Sounds\\Walkassist\\English\\left.wav"));
	//startSound->PlayL();
	}
/** 
 * Destroy child controls.
 */
CTest9Container::~CTest9Container()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CTest9Container
 */
CTest9Container* CTest9Container::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CTest9Container* self = CTest9Container::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CTest9Container
 */
CTest9Container* CTest9Container::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CTest9Container* self = new ( ELeave ) CTest9Container();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CTest9Container::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]
	
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CTest9Container::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CTest9Container::ComponentControl( TInt aIndex ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		}
	// ]]] end generated region [Generated Contents]
	
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CTest9Container::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]
	
	}
				
// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CTest9Container::LayoutControls()
	{
	}
// ]]] end generated function

/**
 *	Handle key events.
 */				
TKeyResponse CTest9Container::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	if ( HandleTest9ContainerOfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	
	// ]]] end generated region [Generated Contents]
	
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CTest9Container::InitializeControlsL()
	{
	
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CTest9Container::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( ETest9ContainerViewId ) )->ClientRect() );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 *	Draw container contents.
 */				
void CTest9Container::Draw( const TRect& aRect ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
	// ]]] end generated region [Generated Contents]
	
	}
	
bool CTest9Container::AddLocation(const TDesC* aOverrideText, TDes* result)
	{
	TBuf<50> aData;
	if(view->RunDataQuery1L(aData, true, aOverrideText) != 0)
		{
		if(result != NULL)
			*result = aData;
		
		return true;
		}
	return false;
	}

void CTest9Container::PauseResumeSwitch()
	{
	IsPaused = !IsPaused;
	_LIT(pause, "Pause");
	_LIT(resume, "Resume");
	if(IsPaused)
		view->RunNote1L(&pause);
	else
		view->RunNote1L(&resume);
	}

void CTest9Container::UpKeyForwardBackwardSwitch()
	{
	IsUpkeyForward = !IsUpkeyForward;
	_LIT(forward, "Up key will move forward");
	_LIT(backward, "Up key will move backward");
	if(IsUpkeyForward)
		view->RunNote1L(&forward);
	else
		view->RunNote1L(&backward);
	}
void CTest9Container::StartNewTrip()
	{
	TBuf<50> aData;
	//player->PlayL();
	TInt res = view->RunConfQuery1L(0);
	//__ASSERT_ALWAYS(false,User::Panic(msg, 0));
	if(res != 0)
		{
		//_LIT(msg, "return KeyNotConsumed");
		//__ASSERT_ALWAYS(false,User::Panic(msg, 0));
		if(AddLocation(0, &aData))
			{
			path = Path::NewL();
			path->start.name = aData;
			ApplicationMode = BuildingPath;
			}
		}
	}
void CTest9Container::AddLocationInTrip()
	{
	TBuf<50> aData;
	_LIT(locationName, "Please enter location name");
	if(AddLocation(&locationName, &aData))
		{
		path->end.name = aData; 
		currentTrip.paths.Append(path);
		path = Path::NewL();
		path->start.name = aData;
		}
	}
void CTest9Container::SaveTrip()
	{
	TBuf<50> aData;
	_LIT(locationName, "Please enter destination name");
	if(AddLocation(&locationName, &aData))
		{
		path->end.name = aData;
		currentTrip.paths.Append(path);
		currentTrip.Save(iEikonEnv->FsSession());
		ApplicationMode = NewTrip;
		}
	}

/** 
 * Handle the offerKeyEvent event.
 */

TKeyResponse CTest9Container::HandleTest9ContainerOfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// TODO: implement offerKeyEvent event handler
	
	if(aKeyEvent.iScanCode == 50 && aType == EEventKeyUp)	// Num2 Pause \ Resume
		{
		PauseResumeSwitch();
		}
	
	if(IsPaused)
		return EKeyWasConsumed;
	
	if(aKeyEvent.iScanCode == 51 && aType == EEventKeyUp)	// Num3 Upkey moves forward\backward
		{
		UpKeyForwardBackwardSwitch();
		return EKeyWasConsumed;
		}
	
	switch (ApplicationMode)
		{
		case 0:	//NewTrip .. I think it should be changed to be MainMenu
			{
			if(aKeyEvent.iCode == EKeyUpArrow)	// Adding EventKeyUp condition (to fire only when the button releases)
				{
				StartNewTrip();
				return EKeyWasConsumed;
				}
			break;
		case 1:		//BuldingPath
			if(aKeyEvent.iRepeats == 0 && aKeyEvent.iCode == EKeyUpArrow)	// Adding EventKeyUp condition (to fire only when the button releases)
				{
				if(IsUpkeyForward)
					path->Forward();
				else
					path->Backward();
				return EKeyWasConsumed;
				}
			if(aKeyEvent.iCode == EKeyRightArrow)
				{
				path->Right();
				//rightSound->PlayL();
				return EKeyWasConsumed;
				}
			if(aKeyEvent.iCode == EKeyLeftArrow)
				{
				path->Left();
				//leftSound->PlayL();
				return EKeyWasConsumed;
				}
			if(aKeyEvent.iScanCode == 49 && aType == EEventKeyUp)	//Add Location	
				{
				AddLocationInTrip();
				return EKeyWasConsumed;
				}
			if(aKeyEvent.iScanCode == 55 && aType == EEventKeyUp)	//Save Trip
				{
				SaveTrip();
				return EKeyWasConsumed;
				}
			break;
			}
		}
	return EKeyWasNotConsumed;
	}
				
